#include "hal_MatrixKeys.h"
#include "STC8G_H_GPIO.h"
#include "STC8G_H_EEPROM.h"



#define COL1 P03//列
#define COL2 P06
#define COL3 P07
#define COL4 P17
#define ROW1 P34//列
#define ROW2 P35
#define ROW3 P40
#define ROW4 P41


#define MatrixKeys_GPIO_PORT0 GPIO_P0
#define MatrixKeys_GPIO_PORT1 GPIO_P1
#define MatrixKeys_GPIO_PORT3 GPIO_P3
#define MatrixKeys_GPIO_PORT4 GPIO_P4
#define MatrixKeys_GPIO_PIN0  GPIO_Pin_3|GPIO_Pin_6|GPIO_Pin_7
#define MatrixKeys_GPIO_PIN1  GPIO_Pin_7
#define MatrixKeys_GPIO_PIN3  GPIO_Pin_4|GPIO_Pin_5
#define MatrixKeys_GPIO_PIN4  GPIO_Pin_0|GPIO_Pin_1

void GPIO_Config() {
    GPIO_InitTypeDef GPIO_InitStructure;        // 结构定义
    GPIO_InitStructure.Mode = GPIO_PullUp;        // 指定IO的输入或输出方式,GPIO_PullUp,GPIO_HighZ,GPIO_OUT_OD,GPIO_OUT_PP
    GPIO_InitStructure.Pin = MatrixKeys_GPIO_PIN0;        // 指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
    GPIO_Inilize(MatrixKeys_GPIO_PORT0, &GPIO_InitStructure); // 初始化
    
    GPIO_InitStructure.Pin = MatrixKeys_GPIO_PIN1;        // 指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
    GPIO_Inilize(MatrixKeys_GPIO_PORT1, &GPIO_InitStructure); // 初始化
    
    GPIO_InitStructure.Pin = MatrixKeys_GPIO_PIN3;        // 指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
    GPIO_Inilize(MatrixKeys_GPIO_PORT3, &GPIO_InitStructure); // 初始化
    
    GPIO_InitStructure.Pin = MatrixKeys_GPIO_PIN4;        // 指定要初始化的IO, GPIO_Pin_0 ~ GPIO_Pin_7
    GPIO_Inilize(MatrixKeys_GPIO_PORT4, &GPIO_InitStructure); // 初始化     
}

void Hal_MatrixKeys_Init() {
    GPIO_Config();
}

/**
 * @brief 行供电状态
 * 
 * @param Row_Id 需要供电的行号
 */
void Hal_SetRowPower(char Row_Id){
    ROW1 = (Row_Id == 0 ? 0 : 1);
    ROW2 = (Row_Id == 1 ? 0 : 1);
    ROW3 = (Row_Id == 2 ? 0 : 1);
    ROW4 = (Row_Id == 3 ? 0 : 1);
}

/**
 * @brief 读取列状态
 * 
 * @param Col_Id 需要读取的列号
 * @return char 返回列状态0或者1
 */
char Hal_ReadColState(char Col_Id) { // 列状态
    if (Col_Id == 0)
        return COL1;
    if (Col_Id == 1)
        return COL2;
    if (Col_Id == 2)
        return COL3;
    if (Col_Id == 3)
        return COL4;
}

/**
 * @brief 读取EEPROM
 * 
 * @param address 
 * @param DataAddress 
 * @param number 
 */
void HAL_ReadEEPROM(unsigned int EE_address, unsigned char *DataAddress, unsigned int number) {
    EEPROM_read_n(EE_address, DataAddress, number);
}

/**
 * @brief 写入EEPROM
 * 
 * @param EE_address 
 * @param DataAddress 
 * @param number 
 */
void HAL_WriteEEPROM(unsigned int EE_address, unsigned char *DataAddress, unsigned int number){
    EEPROM_write_n(EE_address, DataAddress, number);
}

void HAL_EEPROMErase(unsigned int EE_address){
    EEPROM_SectorErase(EE_address);
}











